/*!
  \file
  Header file for a simple HHV on the CPU
*/

/*
  Copyright (c) 2011, President & Fellows of Harvard College
  All rights reserved.
  
  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  
  * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  * Neither the name of the Harvard College nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#ifndef SCIGPU_HHV_HHVOLUME_SIMPLE_HPP
#define SCIGPU_HHV_HHVOLUME_SIMPLE_HPP

#include <vector>
#include <cstring>


#include "hhvolume.hpp"
#include "objectconvertor.hpp"

namespace SciGPU {
  namespace HHV {

    //! Simple heterogeneous hypervolume implementation on the CPU
    /*!
      This class is a minimal implementation of a hypervolume on the
      CPU.
      It uses simple (unaligned, unpadded) linear memory, provided
      through \c std::vector.
      It provides a \c convertor member class
      which can be used for dynamically converting instances of
      HHVolume into instances of HHVolumeSimple.
    */
    template<typename T, unsigned int nDims>
    class HHVolumeSimple : public HHVolume<T,nDims> {
    public:
      //! Expose the type of data stored
      typedef T value_type;

	typedef ObjectConvertor< HHVolumeSimple<T,nDims>, HHVolume<T,nDims> > convertor;
													   
      //! Constructor with buffer
      HHVolumeSimple( const BufferObject<T>& buff = SimpleBuffer<T>() )
        : HHVolume<T,nDims>(buff),
          data() {};

      //! Destructor
      virtual ~HHVolumeSimple( void ) {};


      // -------------------------------
  
      //! Send data to the internal vector
      virtual void Send( void ) {
        memcpy( &this->data[0], this->buffer->start(), this->hyxels() * sizeof(T) );
      }

      //! Fetch data from internal vector
      virtual void Recv( void ) const {
        memcpy( this->buffer->start(), &this->data[0], this->hyxels() * sizeof(T) );
      }

      // ------------------------------

      //! Access the data (const)
      virtual const T* start( void ) const {
        return( &(this->data[0]) );
      }

      //! Access the data
      virtual T* start( void ) {
        return( &(this->data[0]) );
      }


      // -------------------------------


    protected:
      //! The actual data on the CPU
      std::vector<T> data;
      
      //! Allocation of the actual data
      virtual void AllocateDevice( const DimensionObject<nDims>& myDims ) {
        this->data.resize( myDims.hyxels() );
      }

    private:
      
    };


  }
}


#endif
